package cn.com.dcsgo.springcloudstudy.commons.utils;

import java.io.IOException;
import java.net.ServerSocket;

/**
 * 查找可用端口
 *
 * @author Dcsgo
 * @since 2024-08-06
 */

public class PortUtil {
    public static int findFreePort() {
        try (ServerSocket socket = new ServerSocket(0)) {
            return socket.getLocalPort();
        } catch (IOException e) {
            throw new RuntimeException("No available ports", e);
        }
    }
}
